{{extend '../_layouts/home.html'}}

{{block 'title'}}{{'多人博客 - 首页'}}{{/block}}

{{block 'head'}}
<link rel="stylesheet" href="/public/css/markdown-github.css">
<style>
  .panel .panel-heading .action i {
    margin-right: 10px;
  }
</style>
{{/block}}

{{block 'body'}}
<section class="container">
  <div class="row">
    <div class="col-md-9">
      <article class="markdown-body">
      <h1 id="_1"><a name="user-content-_1" href="#_1" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>{{ topic.title }}</h1>
      <blockquote>
        <p>{{topic.topic}}</p>
      </blockquote>
      {{if user &&  topic.user_id.toString() === user._id.toString() }}
      <div class="row pull-right">
        <a  class="btn btn-primary" href="{{'/user/topic/edit/'+ topic._id}}">编缉</a>
        <a  class="btn btn-danger" id="delete_btn" href="{{'/user/topic/delete/'+ topic._id}}">删除</a>
      </div>
      {{/if}}
    </article>
</section>
{{/block}}
{{block 'script'}}
<script>
$('#delete_btn').on('click', function(e){
  e.preventDefault();
  $(".modal-content .modal-body").html(`
  <strong>您确定要注销账号码？<strong><br/><br/>
  <p>
    <button class="btn btn-danger" id="#confirm-del" data-dismiss="modal">删除</button>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <button class="btn btn-primary" data-dismiss="modal">按 错 了</button>   
  </p>
  `);
  $("#myModal").trigger('click');
});

//把点击确定注销的事件，注删到modal上
$("#myModal").next('.modal').delegate('button', 'click',function(e){
  //点击的是 注销按钮，发的注销清求
  if($(this).attr('id') === '#confirm-del'){
    $.ajax({
      url: $('#delete_btn').attr("href"),
      type: 'get',
      success: function(data){
        var err_code = data.err_code
        if (err_code === 0) {
          $(".modal-content .modal-body").html(data.message);
          $("#myModal").trigger('click');
          window.setTimeout(function(){
              window.location.href = '/'
          }, 2000);
        } else if ( err_code === 1 ){
          $(".modal-content .modal-body").html(data.message);
          $("#myModal").trigger('click');
        }else if( err_code === 2){
          $(".modal-content .modal-body").html(data.message);
          $("#myModal").trigger('click');
        }
      }
    })
  }
  
})

</script>
{{/block}}